<?php
namespace Modules\Activity\Entities\YiFanShang;

use Modules\Core\Entities\BaseEntity as Model;

use Setting;

class Record extends Model
{
	protected $guarded = [];
    static public $resourcename = '抽奖记录';
    protected $table = 'yfs_records';

    protected $casts = [
        'sku' => 'json',
        'user' => 'json',
    ];

    public function scopeTag($query, $tag)
    {
        if ($tag === 'free_order') {
            return $query->where('free_order_total', '>', 0);
        }
        elseif ($tag === 'all') {
            return $query;
        }
        else {
            return $query->where('sku_id', intVal($tag));
            // return $query->where('')
        }
    }

    static public function beforeGetList($options)
    {
        if (@$options['tag']) {
            $options['scopes']['tag'] = $options['tag'];
        }

        return $options;
    }

    // 创建榜单
    static public function createWith($package, $order)
    {
    	$user = $order->user;

    	$userInfo = [
    		'name' => $user->name,
    		'headimg' => $user->headimg
    	];

        $arr = [];
        
    	foreach ($package->skus as $packageSku) {

    		$skuInfo = [
    			'title' => $packageSku->title,
    			'thumb' => $packageSku->thumb
    		];

    		array_push($arr, [
    			'uuid' => uniqid(),
    			'user_id' => $user->id,
    			'sku_id' => $packageSku->sku_id,
    			'user' => json_encode($userInfo),
    			'sku' => json_encode($skuInfo),
    			'level' => $packageSku->sku_level,
                'node_id' => $packageSku->node_id,
                'total' => $packageSku->total,
                'free_order_total' => $packageSku->free_order_total ?: 0,
    			// 'is_special_sku' => $packageSku->is_special_sku,
    			'open_index' => $packageSku->open_index,
    			'room_id' => $packageSku->room_id,
    			'created_at' => now(),
    			'updated_at' => now(),
                'shang_type' => @$packageSku->options['shang_type'] ?: 0,
                'shang_title' => @$packageSku->options['shang_title'] ?: ''
    		]);
    	}

    	self::insert($arr);
    }

    // 创建榜单
    static public function createWithPackageSku($packageSku, $user)
    {
        $userInfo = [
            'name' => $user->name,
            'headimg' => $user->headimg
        ];

        $arr = [];

        $skuInfo = [
            'title' => $packageSku->title,
            'thumb' => $packageSku->thumb
        ];

        array_push($arr, [
            'uuid' => uniqid(),
            'user_id' => $user->id,
            'sku_id' => $packageSku->sku_id,
            'user' => json_encode($userInfo),
            'sku' => json_encode($skuInfo),
            'level' => $packageSku->sku_level ?: 0,
            'node_id' => $packageSku->node_id,
            'total' => $packageSku->total,
            'free_order_total' => $packageSku->free_order_total ?: 0,
            // 'is_special_sku' => $packageSku->is_special_sku,
            'open_index' => $packageSku->open_index,
            'room_id' => $packageSku->room_id,
            'created_at' => now(),
            'updated_at' => now(),
            'shang_type' => @$packageSku->options['shang_type'] ?: 0,
            'shang_title' => @$packageSku->options['shang_title'] ?: ''
        ]);

        self::insert($arr);
    }
}
